package travelDream.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import traveldream.ejb.DTO.ExcursionDTO;
import traveldream.ejb.interfaces.ExcursionBeanLocal;
import traveldream.ejb.interfaces.NoDeleteException;
import traveldream.ejb.interfaces.NotFindException;


@ManagedBean(name = "ExcursionManaged")
@ViewScoped
public class ExcursionManaged {
	private List<ExcursionDTO> eDTO;
	private List<ExcursionDTO> filteredExcursion;
	private ExcursionDTO viewedE;
	
	@EJB
	private ExcursionBeanLocal excursionBeanLocal;
	

	public ExcursionManaged(){}
	
	public void allExcursion(){
		eDTO = excursionBeanLocal.getAllExcursion();
	}
	
	public void handleError(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String passedParameter = (facesContext.getExternalContext().getRequestParameterMap().get("error"));
		if(passedParameter==null) return;
		if(passedParameter.compareTo("nullPointer")==0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "An error occured, please try again!!"));
		}
	}
	
	public void searchById() {
		try{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		int passedParameter = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));
		viewedE = excursionBeanLocal.find(passedParameter);
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, use the menu to go back to excursions and try again!!"));
		}
	}
	
	public void searchById(int id){
	try{
		viewedE = excursionBeanLocal.find(id);
	}
	catch(NotFindException e){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, use the menu to go back to excursions and try again!!"));
	}
	}
	
	public String changePage(int id){
		return "excursionView.xhtml?faces-redirect=true&id="+id;
	}

	public String editPage(int id){
		return "excursionEdit.xhtml?faces-redirect=true&id="+id;
	}

	public String delete(int id)
	{
		try{
		excursionBeanLocal.delete(id);
		}
		catch(NotFindException e){
			return "excursion.xhtml?faces-redirect=true&error=nullPointer";
		}
		catch(NoDeleteException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You cannot delete!! Please delete fisrt the package in which it is"));
			return "";
		}
		return "excursion.xhtml?faces-redirect=true";
	}
	
	public List<ExcursionDTO> geteDTO() {
		return eDTO;
	}

	public void seteDTO(List<ExcursionDTO> eDTO) {
		this.eDTO = eDTO;
	}

	public List<ExcursionDTO> getFilteredExcursion() {
		return filteredExcursion;
	}

	public void setFilteredExcursion(List<ExcursionDTO> filteredExcursion) {
		this.filteredExcursion = filteredExcursion;
	}

	public ExcursionDTO getViewedE() {
		return viewedE;
	}

	public void setViewedE(ExcursionDTO viewedE) {
		this.viewedE = viewedE;
	}
	
	
	
	
	
}

